package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.ProductDTO;
import com.woniu.mapper.ProductMapper;
import com.woniu.pojo.Product;
import com.woniu.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired(required = false)
    private ProductMapper productMapper;

    @Override
    public PageData searchPage(Integer currentPage, Integer pageSize, ProductDTO productDTO) {
        IPage<Product> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (productDTO.getTitle() != null && productDTO.getTitle().length() > 0)
            queryWrapper.like("title", productDTO.getTitle());
        IPage<Product> searchPage = productMapper.selectPage(page, queryWrapper);
        PageData pageData = new PageData(searchPage.getTotal(), searchPage.getRecords());
        return pageData;
    }
}
